package com.example.hm_dtfkz.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import com.dq.ad.AdManager;
import com.example.hm_dtfkz.R;
import com.example.hm_dtfkz.databinding.DialogAddCoinResultBinding;
import com.example.hm_dtfkz.util.ActivityClickUtil;
import com.example.hm_dtfkz.util.AnimUtil;
import com.example.hm_dtfkz.util.DisplayUtil;
import com.example.hm_dtfkz.util.sound.GoldMusicManager;

/**
 * FileName: DialogAddCoinResult
 * Author: admin
 * Date: 2021/11/16 16:05
 * Description:
 */
public class DialogAddCoinResult extends Dialog implements View.OnClickListener {
    public static int TYPE_ADD_COIN = 0;//获得金币
    public static int TYPE_EXTRA = 1;//额外获得
    public static int TYPE_SIGN = 2;//签到获得
    public static int TYPE_TASK = 3;//任务获得

    private DialogAddCoinResultBinding resultBinding;
    private final Activity activity;
    private OnClickLister clickLister;
    private double money;       //红包券
    private int dialogType = TYPE_ADD_COIN;

    public DialogAddCoinResult(Activity a) {
        super(a, R.style.dialog_loading);
        this.activity = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        resultBinding = DialogAddCoinResultBinding.inflate(getLayoutInflater());
        setContentView(resultBinding.getRoot());
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = DisplayUtil.getScreenWidth(activity);
        lp.height = DisplayUtil.getScreenHeight(activity);
        getWindow().setAttributes(lp);

        setCancelable(false);
        setCanceledOnTouchOutside(false);
        initView();
        initLister();
    }

    @SuppressLint("SetTextI18n")
    private void initView() {
        resultBinding.imgCenter.setImageResource(R.drawable.icon_quan_role);
        resultBinding.tvMoney.setText((int) this.money + "红包券");

        if (dialogType == TYPE_EXTRA) {
            resultBinding.imgTop.setImageResource(R.drawable.ic_dialog_top_5);
//        } else if (dialogType == TYPE_SIGN) {
//            AnimUtil.receiveAnim(activity, resultBinding.imgDoubleReceive);
//        } else if (dialogType == TYPE_TASK) {
//            AnimUtil.receiveAnim(activity, resultBinding.imgDoubleReceive);
        }

        AdManager.getInstance().showNativeAD(activity, resultBinding.layoutNative, DisplayUtil.getScreenWidthDp(activity) - 48, 0, AdManager.NATIVE_AD_TYPE_UP_IMAGE, false, new AdManager.OnNativeVideoADListener() {
            @Override
            public void onSuccess() {
                resultBinding.layoutNative.setBackgroundColor(activity.getResources().getColor(R.color.Color_FFFFFF));
            }

            @Override
            public void onClosed() {
                resultBinding.layoutNative.setBackgroundColor(activity.getResources().getColor(android.R.color.transparent));
            }

            @Override
            public void onError() {

            }

            @Override
            public void onClick() {

            }

            @Override
            public void onShow(View view) {

            }
        });

        AnimUtil.receiveAnim(activity, resultBinding.imgDoubleReceive);
        AnimUtil.rotateAnim(resultBinding.imgCenterBg);

    }

    private void initLister() {
        resultBinding.imgDoubleReceive.setOnClickListener(this);
        resultBinding.imgClose.setOnClickListener(this);
    }

    public DialogAddCoinResult setClickLister(OnClickLister lister) {
        this.clickLister = lister;
        return this;
    }

    public DialogAddCoinResult setType(int type) {
        this.dialogType = type;
        return this;
    }

    public DialogAddCoinResult setMoney(double m) {
        this.money = m;
        return this;
    }

    @Override
    public void onClick(View v) {
        if (ActivityClickUtil.getInstance().isFastClick()) {
            //防止用户快速点击
            return;
        }
        dismiss();
        GoldMusicManager.getInstance(activity).start();

        AdManager.getInstance().showInterstitialAD(activity);

        if (this.clickLister != null) {
            this.clickLister.onReceive();
        }
    }

    public interface OnClickLister {
        void onReceive();
    }
}